package com.campus.gateway.filter;

import com.campus.gateway.config.AuthProperties;
import com.campus.gateway.util.JwtHelper;
import lombok.RequiredArgsConstructor;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.http.HttpStatus;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.stereotype.Component;
import org.springframework.util.AntPathMatcher;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.util.List;

@Component
@RequiredArgsConstructor
public class DoGlobalFilter implements GlobalFilter, Ordered {

    private final AuthProperties authProperties;

    private final AntPathMatcher antPathMatcher=new AntPathMatcher();

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {

        //1.获取request
        ServerHttpRequest request = exchange.getRequest();

        //2.判断是否是需要登录拦截
        if(ieEXclude(request.getPath().toString())){
            //放行
            return chain.filter(exchange);
        }

        //3.获取token
        String token = null;
        List<String> headers = request.getHeaders().get("Authorization");

        if(headers!=null&&headers.size()>0){
            token = headers.get(0);
        }

        //4.校验并解析token
        Integer userId;
        try {
            userId = JwtHelper.getUserId(token);
        }catch (RuntimeException e){
            //拦截 设置响应状态码为401
            ServerHttpResponse response = exchange.getResponse();
            response.setStatusCode(HttpStatus.UNAUTHORIZED);
            return response.setComplete();
        }

        //5.传递用户信息
        String userInfo=userId.toString();
        ServerWebExchange webExchange=exchange.mutate()
                .request(builder -> builder.header("user-info",userInfo))
                .build();
        //6.放行
        return chain.filter(webExchange);
    }

    private boolean ieEXclude(String path) {

        for (String pathPattern : authProperties.getExcludePaths()){
            if(antPathMatcher.match(pathPattern,path)){
                return true;
            }
        }
        return false;
    }

    @Override
    public int getOrder() {
        return 0;
    }

}
